package com.ruihang.generalibrary.ui.widget;

import android.content.Context;
import android.graphics.Rect;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;

import com.ruihang.generalibrary.utils.DensityUtil;

/**
 * Created by Administrator on 2017/4/18.
 */

public class IntervalDecoration extends RecyclerView.ItemDecoration {

    private int horInterval;
    private int verInterval;
    /**
     * 标签标记，用以区分不同的间距类型
     */
    private int modeTag;
    /**
     * 是否需要不绘制最右边的
     */
    private boolean isIgnorFarRight = true;
    /**
     * 是否需要不绘制最下边的
     */
    private boolean isIgnorFarBottom = true;

    public IntervalDecoration(int horInterval, int verInterval) {
        this.horInterval = horInterval;
        this.verInterval = verInterval;
    }

    public IntervalDecoration(Context context) {
        this(context, 10, 10);
    }

    public IntervalDecoration(Context context, float horInervalDp, float verIntervalDp) {
        horInterval = DensityUtil.dip2px(context, horInervalDp);
        verInterval = DensityUtil.dip2px(context, verIntervalDp);
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        int itemPosition = ((RecyclerView.LayoutParams) view.getLayoutParams()).getViewLayoutPosition();
        int spanCount = getSpanCount(parent);
        int childCount = parent.getAdapter().getItemCount();
        int hor = horInterval;
        int ver = verInterval;
        boolean lar = isIgnorFarBottom && ver > 0 && isLastRaw(parent, itemPosition, spanCount, childCount);
        boolean lac = isIgnorFarRight && hor > 0 && isLastColum(parent, itemPosition, spanCount, childCount);
        if (lar) {// 如果是最后一行，则不需要绘制底部
//            outRect.set(0, 0, horInterval, 0);
            ver = 0;
        }
        if (lac) {// 如果是最后一列，则不需要绘制右边
            hor = 0;
        }
        outRect.set(0, 0, hor, ver);
//        JLog.e("hori=" + horInterval + "  veri=" + verInterval + "\nhor =" + hor + "  ver =" + ver
//                + "  itemPos=" + itemPosition + "  span count=" + spanCount + "  childC=" + childCount
//                + "is laR=" + lar + "  is laC=" + lac);
    }

    public int getModeTag() {
        return modeTag;
    }

    public IntervalDecoration setModeTag(int modeTag) {
        this.modeTag = modeTag;
        return this;
    }

    public void setInterval(int horInterval, int verInterval) {
        this.horInterval = horInterval;
        this.verInterval = verInterval;
    }

    public void setInterval(Context context, float horInervalDp, float verIntervalDp) {
        horInterval = DensityUtil.dip2px(context, horInervalDp);
        verInterval = DensityUtil.dip2px(context, verIntervalDp);
    }

    public boolean isIgnorFarRight() {
        return isIgnorFarRight;
    }

    public IntervalDecoration setIgnorFarRight(boolean ignorFarRight) {
        isIgnorFarRight = ignorFarRight;
        return this;
    }

    public boolean isIgnorFarBottom() {
        return isIgnorFarBottom;
    }

    public IntervalDecoration setIgnorFarBottom(boolean ignorFarBottom) {
        isIgnorFarBottom = ignorFarBottom;
        return this;
    }

    private boolean isLastColum(RecyclerView parent, int pos, int spanCount, int childCount) {
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            if ((pos + 1) % spanCount == 0)// 如果是最后一列，则不需要绘制右边
            {
                return true;
            }
        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
            int orientation = ((StaggeredGridLayoutManager) layoutManager)
                    .getOrientation();
            if (orientation == StaggeredGridLayoutManager.VERTICAL) {
                if ((pos + 1) % spanCount == 0)// 如果是最后一列，则不需要绘制右边
                {
                    return true;
                }
            } else {
                childCount = childCount - childCount % spanCount;
                if (pos >= childCount)// 如果是最后一列，则不需要绘制右边
                    return true;
            }
        }
        return pos == childCount - 1;
    }

    private boolean isLastRaw(RecyclerView parent, int pos, int spanCount, int childCount) {
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            childCount = childCount - childCount % spanCount;
            if (pos >= childCount)// 如果是最后一行，则不需要绘制底部
                return true;
        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
            int orientation = ((StaggeredGridLayoutManager) layoutManager)
                    .getOrientation();
            // StaggeredGridLayoutManager 且纵向滚动
            if (orientation == StaggeredGridLayoutManager.VERTICAL) {
                childCount = childCount - childCount % spanCount;
                // 如果是最后一行，则不需要绘制底部
                if (pos >= childCount)
                    return true;
            } else
            // StaggeredGridLayoutManager 且横向滚动
            {
                // 如果是最后一行，则不需要绘制底部
                if ((pos + 1) % spanCount == 0) {
                    return true;
                }
            }
        }
        return pos == childCount - 1;
    }

    private int getSpanCount(RecyclerView parent) {
        // 列数
        int spanCount = -1;
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {

            spanCount = ((GridLayoutManager) layoutManager).getSpanCount();
        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
            spanCount = ((StaggeredGridLayoutManager) layoutManager)
                    .getSpanCount();
        }
        return spanCount;
    }
}
